//
//  StrategyInspector.m
//  StrategyInspector
//
//  Created by jiro on 12/07/29.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "StrategyInspector.h"

#import "StrategyParameter.h"
#import "StrategyPerfomance.h"
#import "Commons.h"

void StrategyInspector::insertParameterSet( Strategy& strategy, const FxInvestmentBasicParameterSet& s, NSDate* startDate, NSDate* endDate )
{
	DbOperator* ope = [ DbOperator defaultOperator ];
	StrategyParameterSet* parameterSet = strategy.createParameterSet( const_cast< FxInvestmentBasicParameterSet* >( & s ), startDate, endDate );
	const int parameterCount = parameterSet->getParameterCount();
	for ( int i = 0; i < parameterCount; i++ )
	{
		for ( int j = 0; j < parameterCount; j++ )
		{
			StrategyParameter& p = parameterSet->getParameterAtIndex( j );
			p.setValueMiddle();
		}
		
		StrategyParameter& p = parameterSet->getParameterAtIndex( i );
		if ( p.isMutable() )
		{
			continue;
		}

		bool isLast = false;
		while ( isLast == false )
		{
			NSString* s = parameterSet->queryInsertWithSerialValue();
			PGresult* res;
			if ( DbResponse_Success != [ ope executeQuery: s intoResult: & res ] )
			{
				throw( [ [ NSString stringWithString: kText_DbOperationNotSuccess ] cString ] );
			}
			p.nextValue( isLast );
		}
	}
	[ ope disconnect ];
}

void StrategyInspector::inspect( Strategy& strategy, StrategyParameterSet& parameterSet )
{
	StrategyPerfomance performance = strategy.inspect( parameterSet );
	performance.strategyIndex = strategy.getStrategyIndex();
	performance.parameterIndex = parameterSet.getParameterIndex();
}
